package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.DateUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.dao.SysMyScheduleDao;
import com.ccp.dev.system.model.SysMySchedule;
import com.ccp.dev.system.model.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 个人日程 信息操作服务类
 *
 * @author zw
 * @date 2019-07-25 12:11:59
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysMyScheduleService extends BaseService<SysMySchedule> {

    @Resource
    private SysMyScheduleDao sysMyScheduleDao;

    /**
     * 添加日程
     * @param sysMySchedule 日程数据，包含ID
     * @return 新增数量
     */
    @Transactional(rollbackFor = Exception.class)
    public int addSchedule(SysMySchedule sysMySchedule){
        sysMySchedule = fillData(sysMySchedule,true);
        int result = sysMyScheduleDao.add(sysMySchedule);
        return result;
    }

    /**
     * 查询个人日程
     * @param queryMap 查询条件，必须包含PID，可包含重要程度、是否提醒等查询条件
     * @param startDate 开始日期，yyyy-mm-dd
     * @param endDate 结束日期，yyyy-mm-dd
     * @return
     */
    public List<SysMySchedule> queryMySchedule(Map<String, Object> queryMap , String startDate, String endDate) {
        endDate = endDate+" 23:59:59";
        startDate = startDate+" 00:00:00";
        queryMap.put("startDate", DateUtil.parseDate(startDate));
        queryMap.put("endDate",DateUtil.parseDate(endDate));
        return sysMyScheduleDao.queryMySchedule(queryMap);
    }




    /**
     * 填充用户信息数据
     * @param sysMySchedule 日程数据
     * @param isAdd 是否新增数据
     * @return 填充后的数据
     */
    public SysMySchedule fillData(SysMySchedule sysMySchedule,boolean isAdd){
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        Date currDate = new Date();
        if(isAdd){
            sysMySchedule.setScheduleUser(sysUser.getFullName());
            sysMySchedule.setScheduleUserId(sysUser.getUserId());
            sysMySchedule.setCreateBy(sysUser.getUserId());
            sysMySchedule.setCreateTime(currDate);
        }
        sysMySchedule.setUpdateBy(sysUser.getUserId());
        sysMySchedule.setUpdateTime(currDate);
        return sysMySchedule;
    }


    /**
     * 根据个人日程id查询个人日程name
     * @param ids 参数
     * @return  list
     */
    public List<String> queryScheduleNameScheduleIds(List<String> ids){
        return  sysMyScheduleDao.queryScheduleNameScheduleIds(ids);
    }
}
